import uvicorn
from fastapi import FastAPI
from loguru import logger
from contextlib import asynccontextmanager
from app.db.redis import redis_client
from app.db.mysql import init_mysql
from app.core.config import settings

@asynccontextmanager
async def lifespan(app: FastAPI):
    # app startup
    await redis_client.init_redis()
    async with init_mysql(app):
        yield
    # app teardown
    await redis_client.close_redis()

app = FastAPI(
    title=settings.APP_TITLE,
    version=settings.APP_VERSION,
    lifespan=lifespan
)

if __name__ == "__main__":
    logger.info(f"启动 {settings.APP_TITLE}")
    uvicorn.run(app, host=settings.APP_HOST, port=settings.APP_PORT)
